Ext.namespace("admin.event");

admin.event.event = function(panelId, title, iconClz){
	var eventStore = new Ext.data.JsonStore({
		url : "admin/event/findByCon",
		fields : ["id", "name", "description", "eventDate", "startTime",
			"endTime", "location", "maxSeat", "status", "createDate", "updateDate"],
		totalProperty : "total",
		root : "data",
		autoLoad : true,
		listeners : {
			beforeload : function(s){
				s.removeAll();
                Ext.apply(s.baseParams, {
                    keyword : queryField.getValue(),
                    status : statusCombo.getValue()
                });
			}
		}
	});
	var eventSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var eventCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "活动名称",
		dataIndex : "name",
        width : 300
	}, {
		header : "地点",
		dataIndex : "location",
		width : 280
	}, {
		header : "活动时间",
        dataIndex : "eventDate",
        renderer : renderTime2Minutes,
		width : 150
	}, {
        header : "状态",
        dataIndex : "status",
        renderer : renderEnumStatus
    }, {
		header : "创建日期",
		dataIndex : "createDate",
        renderer : renderDate
	}]);
    var statusCombo = genStatusCombo();
    var queryField = genQueryField();
    var queryBar = ["->", "状态：", statusCombo, "关键词：",  queryField, {
        text : "查询",
        iconCls : "query",
        handler : function(){
            eventStore.load();
        }
    }];
	var eventGrid = new Ext.grid.GridPanel({
		store : eventStore,
		cm : eventCm,
		sm : eventSm,
		frame : true
	});
	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [eventGrid],
        tbar : genToolBar(genOptBarByFn(add, del, update, toggleStatus), queryBar, panelId),
        bbar : genPageBar(eventStore)
	});

	function add() {
            var win = genAddorUpdateWin("admin/event/add");
		win.show();
	}
	function del(){
		var rec = eventSm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择活动！");
			return;
		}
		confirmDelete(remove);
		function remove(){
			ajaxRequest({
				url : "admin/event/delete/" + rec.get("id"),
				callback : function(){
                    eventStore.reload();
				}
			});
		}
	}
	function update(){
		var rec = eventSm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择活动！");
			return;
		}
		var win = genAddorUpdateWin("admin/event/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
	function toggleStatus(){
		var rec = eventSm.getSelected();
		if(rec==null){
			showFailMsg("请选择活动！");
			return;
		}
		ajaxRequest({
			url : "admin/event/toggleStatus/" + rec.get("id"),
			callback : function(){
                eventStore.reload();
			}
		});
	}
	function genAddorUpdateWin(url){
		return new Ext.Window({
			title : "新增或修改活动",
			layout : "fit",
			modal : true,
			iconCls : "save",
			height : 330,
			width : 690,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
		    return new Ext.form.FormPanel({
                url : url,
                labelWidth : 60,
                labelAlign : "right",
                baseCls: 'x-plain',
                layout : "hbox",
                items : [{
                    flex : 1,
                    layout: "form",
                    frame : true,
                    items : [{
                        xtype : "hidden",
                        name : "id",
                        //value设置-1，为空则controller检验失败
                        value : "-1"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "活动名称",
                        allowBlank : false,
                        name : "name"
                    }
                    , {
                        fieldLabel : "活动时间",
                        allowBlank : false,
                        name : "eventDate",
                        xtype : "datetimefield"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "活动地点",
                        allowBlank : false,
                        name : "location"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "新闻链接",
                        allowBlank : false,
                        name : "wxNewsUrl"
                    }, genEnumStatusCombo()
                    ]
                }, {
                    flex : 1,
                    layout: "form",
                    frame : true,
                    items : [{
                        xtype: 'box',
                        width: 320, //图片宽度
                        height: 200, //图片高度
                        id : "event_snapshoot_view",
                        autoEl: {
                            tag: 'img',     //指定为img标签
                            src: ''         //指定url路径
                        }
                    }, {
                        xtype : "textfield",
                        hidden : true,
                        name : "snapshoot"
                    }],
                    buttons : [new Ext.Button({
                        text : "选择微信预览图",
                        handler : uploadSnapshoot
                    })],
                    buttonAlign : "center"
                }],
                buttonAlign : "center",
                buttons : [{
                    text : "保存",
                    iconCls : "save",
                    handler : function(b){
                        submitForm(b.findParentByType("form"), refreshEventGrid);
                        function refreshEventGrid(){

                            eventStore.reload();
                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    type : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
		    function uploadSnapshoot() {
                var form = new Ext.form.FormPanel({
                    fileUpload : true,
                    frame : true,
                    labelWidth: 30,
                    url : "admin/event/snapshoot",
                    defaults : {
                        anchor : "100%",
                        allowBlank : false
                    },
                    items : [{
                        fieldLabel : "文件",
                        emptyText: '请选择文件',
                        anchor: '100%',
                        xtype : "fileuploadfield",
                        name : 'snapshoot',
                        buttonText : "浏览"
                    }],
                    buttons : [{
                        text : "确定",
                        iconCls : "save",
                        handler : function(b){
                            submitForm(b.findParentByType("form"), refreshSnapshoot);
                            function refreshSnapshoot(json){
                                var snapshootPath = json.result.data;
                                // 更新活动快照
                                var view = Ext.getCmp("event_snapshoot_view");
                                view.getEl().dom.src = "resources/temp/" + snapshootPath;
                                // 更新照片
                                view.nextSibling().setValue(snapshootPath);
                            }
                        }
                    }, {
                        text : "重置",
                        iconCls : "reset",
                        handler : function(b){
                            b.findParentByType("form").getForm().reset();
                        }
                    }]
                });
                var win = new Ext.Window({
                    title : "上传文件",
                    width : 300,
                    height : 150,
                    layout : "fit",
                    frame : true,
                    modal : true,
                    items : [form]
                });
                win.show();
            }
		}
	}

}

registerModule("admin.event.event", admin.event.event);
